<?php

$oDb = &Database::get();

$oReq = &App::getRequest();

$oTpl = &App::getTemplate();

App::loadClass('Validator');

if ( $oReq->get('submit') )
{
	if ( !file_exists(App::getParam('path') . "include/languages/phpfox-language_en.php") )
	{
		trigger_error("Cant load main language package.", E_USER_ERROR);
		exit;
	}

	require_once App::getParam('path') . "include/languages/phpfox-language_en.php";

	$aPost = $oReq->getArray('val');

	$aLanguageQuery = array
	(
		'title' => $aPost['title'],
		'userselect' => $aPost['userselect'],
		'languagecode' => $aPost['languagecode'],
		'charset' => $aPost['charset'],
		'direction' => $aPost['direction'],
		'time' => time(),
		'created' => $aPost['created'],
		'site' => $aPost['site'],
	);

	$iId = $oDb->insert(App::getT('language'), $aLanguageQuery);

	foreach($_MSG as $aItem)
	{
		$aQuery = array
		(
			'languageid' => $iId,
			'varname' => $aItem['varname'],
			'text' => $aItem['text'],
			'product' => 'konsort',
			'time' => $aItem['time'],
			'version' => $aItem['version'],
			'module' => $aItem['module'],
			'default' => $aItem['text']
		);
		$oDb->insert(App::getT('language_phrase'), $aQuery);
	}

	foreach($_OPTION as $aItem)
	{
		$aQuery = array
		(
			'languageid' => $iId,
			'varid' => $aItem['varid'],
			'varname' => $aItem['varname'],
			'text' => $aItem['text'],
			'product' => 'konsort',
			'time' => $aItem['time'],
			'default' => $aItem['text']
		);
		$oDb->insert(App::getT('language_options'), $aQuery);
	}

	$oDb->query("OPTIMIZE TABLE `". App::getT('language_options') ."`");
	$oDb->query("OPTIMIZE TABLE `". App::getT('language_phrase') ."`");

	App::rmCache('language_listing.php');

	App::gotoUrl('admin.language.create', array(), App::format('Mod_Language.new_package_created'), false);
}

$aFields = array
(
	'val[title]'   => array('title' => App::format('language_title'), 'def' => 'required'),
	'val[languagecode]'   => array('title' => App::format('laguage_abb_code'), 'def' => 'required'),
	'val[charset]'   => array('title' => App::format('language_charset'), 'def' => 'required'),
);
$oValidator = &new Validator($aFields, array());

$oTpl->assignSrc('sValMess', $oValidator->makeJsMess());
$oTpl->assignSrc('sValJs', $oValidator->makeJS());
$oTpl->assign(array
(
	'aItem' => (isset($aPost) ? $aPost : ''),
));
?>